<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Lab #3: Syntactic Analyzer</title>
<link rel="stylesheet" type="text/css">
</head>

<body>
<font face="Trebuchet MS">
<table width="100%">
<tbody>
  <tr>
<td>
<h2>Lab #3: Syntactic Analyzer</h2>
<hr>

<h3>Introduction</h3>

<p>This lab requires you to write a syntactic analyzer for the 
MiniJava language as described in the Tiger book. In order to do 
this, you will need to figure 
out the grammer, by reading appendix A of the book
<i>Modern Compiler Implementation in Java</i>. </p>

<p>
In this lab, you will be building your parser using ML-Yacc. 
There is online documentation on 
<a href="http://www.smlnj.org/doc/ML-Yacc/index.html">ML-YACC</a> here. 
There is also related material in chapter 3 of 
<i>Modern Compiler Implementation in Java</i>.
</p>

<h3>Start</h3>

<p>
To start, you should download this 
<a href="http://staff.ustc.edu.cn/~bjhua/courses/compiler/2011/labs/lab3/lab3.rar">
package</a>,  
in which we have offered you a code skeleton. Here is a brief 
discription of the files:
</p>
<table valign="top" border="1" >
<tr>
    <td>parser/java.grm  </td>
    <td>
	the ML-Yacc specification file, ML-Yacc will use it to 
	generate a parser 
	</td>
  </tr>
  <tr>
    <td><p>parser/parse.fun</p>
    <p>parser/parse.sig</p> </td>
    <td>the definition of Parse functor, it defines a function 
	<code>parse()</code>, which reads the input file and then 
	calls the lexer and parser</td>
  </tr>
  <tr>
    <td >control/error-msg.sig, .sml</td>
    <td >the definition of error handling facilities, 
    they will be used in the lexer (and later labs)</td>
  </tr>
  <tr>
    <td>main/main.sig, .sml</td>
    <td>the definition of Main structure, it defines a function
	<code>main()</code>, which will call function 
	<code>parse()</code> defined in <code>parser/parse.fun</code>,
	and another function <code>mainWrap()</code> wraps function 
	<code>main()</code></td>
  </tr>
  <tr>
    <td>call-main.sml </td>
    <td>the entry of whole program</td>
  </tr>
  <tr>
    <td>Test.java </td>
    <td>a sample input file to test your parser</td>
  </tr>
  <tr>
    <td>Result.txt </td>
    <td>the output of parsing <code>Test.java</code></td>
  </tr>  
</table>
Especially, there is a lexer file <code>java.lex.sml</code>. If your lexer
from lab #2 does not work properly, then you may use this one; orelse
you may delete this file, and use your own instead.

<h3>Your Job</h3>
<p>
Your job is to read and edit the code in <code>java.grm</code>. You will 
have to remove some of the sample code and add a lot of your 
own. The code you will add falls into the following three categories: 
<ul>
<li>terminals
<li>nonterminals
<li>production rules
</ul>

For the first kind, you should name the teminals the same as those
in the file <code>java.lex</code>, i.e., you should delete the offered
<code>java.lex.sml</code> and generate your own version from
<code>java.lex</code>.

For the second kind, you should not worry about adding semantics value
for now, this is the task in the next lab.

For the third and last kind, you should not attach semantic actions
to the production rules (just as we did for now).

<h3>Compile, run, and test</h3>
<ul>
<li> <b>Generate parser.</b>  After you finish the 
<code>java.grm</code>, you can change the directory to 
<code>lab3/parse/</code>, and then type 
<pre>
ml-yacc java.grm
</pre>
to generate parser. Hopefully, you'd see these files being generated:
<code>java.grm.sig, java.grm.sml</code>; if not, read the error
message and repeat this step. Once successful, you can continue to
do the following steps.
<pre>
</pre>
</li>
<li><b>Compile the whole program.</b>  change the directory 
to lab3, and then run
<pre>
CM.make "sources.cm";
</pre>
as usual. (But, we have offered you a much friendly way to
compile your programs: we have offered a batch script called
<code>build.bat</code>, so you may just want to type
<code>build</code> on prompt.
<pre>
</pre>
</li>
<li><b>Run the whole program.</b> 
Without changing the directory, type
</li>
<pre>
sml @SMLload=jc.x86-win32 Test.java
</pre> 
attention: maybe the path of <code>"Test.java"</code> 
should be changed according your path.  
It will output the same as <code>"Result.txt"</code>. (Also, we
have offered a simple batch file <code>jc.bat</code>, you may
try it. And you may add this to your PATH, if you want to use
it somewhere.)
</ul>

<p>
As always, a single test is far from complete. You will want to write 
your own test cases and 
thoroughly test your parser. Repeat the above procedure once 
you modify your code.
</p>

<h3>Conflicts</h3>
Any forms of conflicts (shift-reduce or reduce-reduce) is now allowed in 
this lab. You may want to explain, in a separate file, what kind of
conflicts you've come across, and how you handle them.

<h3>Handin</h3>
This completes the lab. Hand in your solution to information 
system.

</td>
</tr>
  
</tbody>
</table>
</font>


</body>
</html>
